package com.superiron.scaffold.config;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity implements Serializable {
	
	//主键数据库自增id
	@Id
	@TableId(type = IdType.INPUT)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long id;
	
	//自动赋值创建时间
	@CreatedDate
	@Column(name = "create_time", updatable = false,
			columnDefinition = "DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间'")
	// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date createTime;
	
	//自动赋值更新时间
	@LastModifiedDate
	//设置数据库字段默认值
	@Column(name = "update_time",
			columnDefinition = "DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间'")
	//JSONField 一个是做加密时FastJson序列化用
	//JsonFormat 一个是不加密时框架JackJson转出用
	// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date updateTime;
	
	@Override
	public String toString() {
		return JSONUtil.toJsonPrettyStr(this);
	}
}
